﻿using MT.Common;
using MT.DAL.Abstract;
using MT.DAL.Abstract.UserCenter;
using MT.DAL.Dto.UserCenter;
using MT.Models.Entity;
using MT.Models.Enumerate;
using System;
using System.Threading.Tasks;

namespace MT.DAL.Concrete.UserCenter
{
    public class UserLogService : BaseDbService, IUserLogService
    {

        private readonly UserLoginLogDto _userLoginLogDto;
        private readonly UserOperateLogDto _userOperateLogDto;

        public UserLogService(IServiceConstructorParams serviceConstructorParams, AppDbContext dbContext) : base(serviceConstructorParams, dbContext)
        {
            _userLoginLogDto = new UserLoginLogDto(dbContext);
            _userOperateLogDto = new UserOperateLogDto(dbContext);
        }

        public async Task CreateLoginLogAsync(UserLoginLog userLoginLog)
        {
            using (var conn=new AppDbContext())
            {
                userLoginLog.CreateTime = DateTime.Now;
                await conn.UserLoginLog.AddAsync(userLoginLog);
                await conn.SaveChangesAsync();
            }
        }

        public async Task CreateUserOperateLogAsync(UserOperateLog userOperateLog)
        {
            using (var context = new AppDbContext())
            {
                userOperateLog.CreateTime = DateTime.Now;
                await context.AddAsync(userOperateLog);
                await context.SaveChangesAsync();
            }
   
        }

        public async Task CreateUserOperateLogAsync(string clientTypeDic,string ip, ModelTypeEnum modelTypeEnum,
            OperateTypeEnum operateTypeEnum, int userId,string operateContent,string operateIds)
        {
            await CreateUserOperateLogAsync(new UserOperateLog()
            {
                ClientTypeDic = clientTypeDic,
                CreateTime = DateTime.Now,
                Delete = false,
                InstitutionId = 0,
                Ip = ip,
                ModelType = (byte)modelTypeEnum,
                UserId = userId,
                Type = (byte)operateTypeEnum,
                OperateContent = operateContent,
                OperateIds = operateIds
            });
        }

        public async Task<PageModel<UserLoginLog>> GetrLoginLogPageAsync(int pageIndex, int pageSize, string name, DateTime? startTime, DateTime? endTeime)
        {
            return await _userLoginLogDto.GetPageAsync(pageIndex, pageSize, name, startTime, endTeime);
        }

        public async Task<PageModel<UserOperateLog>> GetOperateLogPageAsync(int pageIndex, int pageSize, string name, DateTime? startTime, DateTime? endTeime)
        {
            return await _userOperateLogDto.GetPageAsync(pageIndex, pageSize, name, startTime, endTeime);
        }
    }
}
